package com.xiaoq.feature.repository.impl;

import java.util.ArrayList;
import java.util.List;

import com.xiaoq.coms.util.MathUtil;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.coms.util.IdGeneratorUtils;
import com.xiaoq.feature.entity.ShopFoodEntity;
import com.xiaoq.feature.repository.IShopFoodRepository;
import com.xiaoq.feature.repository.IVegetRecomRepository;
import com.xiaoq.vo.PagingVo;

@Repository
public class ShopFoodRepositoryImpl implements IShopFoodRepository {
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private IVegetRecomRepository vegetRecomRep;
	
	/**
	 * 将这个矩形的长度转化成经度差常量 111KM/1度
	 */
	private Double dislongitude = 0.02;
	
	/**
	 * 将这个矩形的宽度的纬度差常量
	 */
	private Double dislatitude = 0.02;

	@Autowired
	private Datastore datastore;
	
	private static final Logger LOG = LoggerFactory.getLogger(ShopFoodRepositoryImpl.class);
	
	@Override
	public boolean save(ShopFoodEntity shopFood) {
		String id = shopFood.getId();
		if(id==null){
			id = idGenerator.nextId();
			shopFood.setId(id);
		}
		
		datastore.save(shopFood);
		return true;
	}

	
	/**
	 * 
	 * 获取景区周围的必吃商店列表这里就用 矩形长宽5公里
	 */
	@Override
	public List<ShopFoodEntity> findShopFoodList(Double longitude, Double latitude, Double localLongitude,Double localLatitude,PagingVo pvo) {
//		int ps = pvo.getPs();
//		int pn = (pvo.getPn() - 1) * ps;
		Double dislongitude = 0.02;
		Double dislatitude = 0.02;
		List<ShopFoodEntity> shopFoodList = null;
		Long total = 0L;
		while (true) {
			shopFoodList = new ArrayList<>();
			Double longitudeAddtion = longitude + dislongitude;
			Double longitudeLessen = longitude - dislongitude;
			Double latitudeAddtion = latitude + dislatitude;
			Double latitudeLessen = latitude - dislatitude;
			Query<ShopFoodEntity> shopFoodQuery = datastore.find(ShopFoodEntity.class);
			shopFoodQuery.filter("longitude <", longitudeAddtion).filter("longitude >", longitudeLessen)
					.filter("latitude >", latitudeLessen).filter("latitude <", latitudeAddtion)
					.order("-avgScore");
			total = shopFoodQuery.count();

			if(total > 0){
				if(total >= 90){
					shopFoodList = shopFoodQuery.asList();
					break;
				}
				if (dislongitude >= 0.06){
					shopFoodList = shopFoodQuery.asList();
					break;
				}
			}

			dislongitude = dislongitude + 0.02;
			dislatitude = dislatitude + 0.02;
			// shopFoodQuery.field("loc").near(localLongitude, localLatitude);
			// shopFoodQuery.offset(pn).limit(ps);

		}
		pvo.setTc(total);
		return shopFoodList;
	}
	
	@Override
	public List<ShopFoodEntity> findShopFoodListByCity(String cityCode, PagingVo pvo) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		List<ShopFoodEntity> shopFoodList = null;
		Long total = 0L;
		shopFoodList = new ArrayList<>();
		Query<ShopFoodEntity> shopFoodQuery = datastore.find(ShopFoodEntity.class);
		shopFoodQuery.field("zoneCity").equal(cityCode).order("-avgScore");
		total = shopFoodQuery.count();
		shopFoodQuery.offset(pn).limit(ps);
		shopFoodList = shopFoodQuery.asList();

		pvo.setTc(total);
		return shopFoodList;
	}


	@Override
	public ShopFoodEntity findShopFood(String id) {
		Query<ShopFoodEntity> shopFoodQuery = datastore.find(ShopFoodEntity.class).field(IdGeneratorUtils.IDGENERATOR).equal(id);
		return shopFoodQuery.get();
	}


	@Override
	public List<ShopFoodEntity> findShopFoodList(Double longitude, Double latitude) {
		Double longitudeAddtion = longitude + dislongitude;
		Double longitudeLessen = longitude - dislongitude;
		Double latitudeAddtion = latitude + dislatitude;
		Double latitudeLessen = latitude - dislatitude;
		Query<ShopFoodEntity> shopFoodQuery = datastore.find(ShopFoodEntity.class);
		shopFoodQuery
		.filter("longitude <",longitudeAddtion)
		.filter("longitude >",longitudeLessen)
		.filter("latitude >",latitudeLessen)
		.filter("latitude <",latitudeAddtion)
				.order("-avgScore");
		List<ShopFoodEntity> shopFoodList = shopFoodQuery.asList();

		if(shopFoodList == null){
			return new ArrayList<>();
		}
		return shopFoodList;
	}


	@Override
	public void addLoc() {
		Query<ShopFoodEntity> shopFoodQuery = datastore.find(ShopFoodEntity.class);
		List<ShopFoodEntity> shopFoodList = shopFoodQuery.asList();
		for(ShopFoodEntity shop:shopFoodList){
			List<Double> loc = new ArrayList<>();
			loc.add(shop.getLongitude());
			loc.add(shop.getLatitude());
//			shop.setLoc(loc);
			datastore.save(shop);
		}
	}


	@Override
	public List<ShopFoodEntity> findShoopFoodListByIds(List<String> shopIds) {
		Query<ShopFoodEntity> shopFoodQuery = datastore.find(ShopFoodEntity.class);
		shopFoodQuery.filter("_id in", shopIds);
		return shopFoodQuery.asList();
	}

	@Override
	public List<ShopFoodEntity> getShopFoodByPoint(double latitude, double longitude, long nearM, PagingVo pagingVo) {

		double distance = nearM / MathUtil.METER_2_MONGO_DISTANCE;
		Query<ShopFoodEntity> shopFoodQuery =  datastore.find(ShopFoodEntity.class)
				.field("loc").near(longitude, latitude, distance);
		if (pagingVo != null){
			shopFoodQuery.offset(pagingVo.getPageOffset()).limit(pagingVo.getPs());
		}
		return shopFoodQuery.asList();
	}


	@Override
	public boolean deleteShopFoodById(String shoptFoodId) {
		Query<ShopFoodEntity> shopFoodQuery = datastore.find(ShopFoodEntity.class).field(IdGeneratorUtils.IDGENERATOR).equal(shoptFoodId);
		datastore.delete(shopFoodQuery);
		return true;
	}

}
